package com.aar.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import domain.Child;
import domain.ChildDAO;
import domain.Gift;
import domain.GiftDAO;
 
@Path("/annee")
public class MethodeREST {
	
	final static String text_html_header = "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'><html><head><meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'><link rel='stylesheet' href='http://localhost:8080/PROJET-HAMEK-SAIDI-ARR/styles/bootstrap.css'><link rel='stylesheet' href='http://localhost:8080/PROJET-HAMEK-SAIDI-ARR/styles/bootstrap-responsive.css'><link rel='stylesheet' href='http://localhost:8080/PROJET-HAMEK-SAIDI-ARR/styles/apprise.css' type='text/css'><link rel='stylesheet' href='http://localhost:8080/PROJET-HAMEK-SAIDI-ARR/styles/style.css'><title></title></head><body><div class='container'><div class='row'><div class='span12'><div class='page-header'><h1>Site de Gestion de Commande</h1></div></div></div><div class='row'><div class='span2' id='menu'><ul class='nav nav-list'><li><a href='http://localhost:9999/aar/annee/enfants'>Gerer les enfants</a></li></ul><ul class='nav nav-list'><li><a href='http://localhost:8080/PROJET-HAMEK-SAIDI-ARR/newChild.html'>Creation de compte</a></li></ul><ul class='nav nav-list'><li><a href='http://localhost:8080/PROJET-HAMEK-SAIDI-ARR/searchChild.html'>Chercher un enfant</a></li></ul><ul class='nav nav-list'><li><a href='http://localhost:8080/PROJET-HAMEK-SAIDI-ARR/searchGift.html'>Chercher un cadeau</a></li></ul></div><div class='span10'>";
	final static String text_html_footer = "</div></div><div class='row'><div class='span12' id='footer'><p>Copyright &copy; 2013 gestion-commande.fr Tous droits reserves</p></div></div></div></body></html>";
	final static String success_pre = "<div class='alert alert-block span9 alert-success'><h4>";
	final static String success_post = "</h4>";
	
	@GET
	@Path("/enfants")
	@Produces(MediaType.TEXT_XML)
	public String getAllChildren(){
		String result = "";
		ChildDAO dao = new ChildDAO();
        List<Child> childs = dao.getAllChilds();
		for(int i=0;i<childs.size();i++){
			result += "<enfant>" +  "<id>" + childs.get(i).getId()+ "</id>" +
					  "<nom>" + childs.get(i).getName()+ "</nom>" +
					  "<ville>" + childs.get(i).getCity() + "</ville>" +
					  "<estSage>" + childs.get(i).getIsWise() + "</estSage></enfant>"; 
		}
		return "<?xml version =\"1.0\"?>" + "<enfants>"+ result +"</enfants>";
	}
	
	@GET
    @Path("/enfants/{idEnfant}/")
    @Produces(MediaType.TEXT_XML)
    public String getChild(@PathParam("idEnfant") long idEnfant) {
		String result = "";
		ChildDAO dao = new ChildDAO();
        Child child = dao.getChild(idEnfant);
		result += "<enfant><nom>" + child.getName() + "</nom>" +
				  "<ville>" + child.getCity() + "</ville>" +
			      "<estSage>" + child.getIsWise() + "</estSage></enfant>"; 
		return "<?xml version =\"1.0\"?>" + result;
    }
	
	@GET
	@Path("/enfants/{idEnfant}/cadeaux")
	@Produces(MediaType.TEXT_XML)
	public String getAllGiftsFromChild(@PathParam("idEnfant") long idChild){
		String result = "";
		GiftDAO dao = new GiftDAO();
		List<Gift> gifts = dao.getGiftsByChild(idChild);
		
		for(int i=0;i<gifts.size();i++){
			result += "<cadeau><id>"+gifts.get(i).getId()+"</id>" +
					  "<nom>" + gifts.get(i).getName() + "</nom>" +
					  "<valider>" + gifts.get(i).getIsValid() +  "</valider>" +
				      "<fabriquer>" + gifts.get(i).getIsCreate() +  "</fabriquer></cadeau>"; 
		}
		return "<?xml version =\"1.0\"?>" + "<cadeaux>"+ result +"</cadeaux>";
	}
	
	@GET
	@Path("/cadeaux")
	@Produces(MediaType.TEXT_XML)
	public String getAllGifts(){
		String result = "";
		GiftDAO dao = new GiftDAO();
		List<Gift> gifts = dao.getAllGifts();
		
		for(int i=0;i<gifts.size();i++){
			result += "<cadeau><id>"+gifts.get(i).getId()+"</id>" +
					  "<nom>" + gifts.get(i).getName() + "</nom>" +
					  "<valider>" + gifts.get(i).getIsValid() +  "</valider>" +
				      "<fabriquer>" + gifts.get(i).getIsCreate() +  "</fabriquer></cadeau>"; 
		}
		return "<?xml version =\"1.0\"?>" + "<cadeaux>"+ result +"</cadeaux>";
	}
	
	@GET
	@Path("/enfants/{idEnfant}/cadeaux/{idCadeau}")
	@Produces(MediaType.TEXT_XML)
	public String getGiftFromChild(@PathParam("idEnfant") long idChild, @PathParam("idCadeau") long idGift){
		String result = "";
		GiftDAO dao = new GiftDAO();
		Gift gifts = dao.getGiftByChild(idChild, idGift);
		
		result += "<cadeau><id>"+gifts.getId()+"</id>" +
				  "<nom>" + gifts.getName() + "</nom>" +
				  "<valider>" + gifts.getIsValid() +  "</valider>" +
				  "<fabriquer>" + gifts.getIsCreate() +  "</fabriquer></cadeau>"; 
		return "<?xml version =\"1.0\"?>" + "<cadeaux>"+ result +"</cadeaux>";
	}	
	
	@GET
	@Path("/Enfants")
	@Produces(MediaType.TEXT_XML)
	public String getChildsByALL(@QueryParam("nom") String name,
								 @QueryParam("ville") String city,
								 @QueryParam("estSage") String IsWise){
		String result = "";		
		ChildDAO dao = new ChildDAO();
		List<Child> childs = null;
		childs = dao.getChildsByAll(name, city, IsWise);
		for(int i=0;i<childs.size();i++){
			result += "<enfant><id>"+ childs.get(i).getId()+" </id>" +
					  "<nom>" + childs.get(i).getName()+ "</nom>" +
					  "<ville>" + childs.get(i).getCity() + "</ville>" +
					  "<estSage>" + childs.get(i).getIsWise() + "</estSage></enfant>"; 
		}
		return "<?xml version =\"1.0\"?>" + "<enfants>"+ result +"</enfants>";
	}
	
	//@PUT
	@POST
	@Path("/cadeaux/{idCadeau}")
	@Produces(MediaType.TEXT_XML)
	public String updateGiftFromChild(@PathParam("idCadeau") long idGift,
									  @FormParam("fabriquer") boolean isCreate){
		GiftDAO dao = new GiftDAO();
		dao.updateGiftCreate(idGift, isCreate);
		return "<?xml version =\"1.0\"?>" + "<response>201</response>";
	}
	
	@GET
	@Path("Cadeaux")
	@Produces(MediaType.TEXT_XML)
	public String getGiftFromChild( @QueryParam("nom") String name,
			 						@QueryParam("valider") String isValide,
			 						@QueryParam("fabriquer") String isCreate){
		String result = "";
		GiftDAO dao = new GiftDAO();
		List<Gift> gifts = dao.getGiftByAll(name, isValide, isCreate);
		
		for(int i=0;i<gifts.size();i++){
			result += "<cadeau><id>"+gifts.get(i).getId()+"</id>" +
					  "<nom>" + gifts.get(i).getName() + "</nom>" +
					  "<valider>" + gifts.get(i).getIsValid() +  "</valider>" +
				      "<fabriquer>" + gifts.get(i).getIsCreate() +  "</fabriquer></cadeau>"; 
		}
		return "<?xml version =\"1.0\"?>" + "<cadeaux>"+ result +"</cadeaux>";
	}
	
	@GET
	@Path("/enfants/{idEnfant}/cadeaux")
	@Produces(MediaType.TEXT_HTML)
	public String getAllGiftsFromChildHtml(@PathParam("idEnfant") long idChild){
		String result = "";
		GiftDAO dao = new GiftDAO();
		List<Gift> gifts = dao.getGiftsByChild(idChild);	
		for(int i=0;i<gifts.size();i++){
			result += "<tr class='bandeau'><td>"+gifts.get(i).getName()+"</td><td>"+gifts.get(i).getIsValid()+"</td><td>"+gifts.get(i).getIsCreate()+
					  "</td><td><form action=\"http://localhost:9999/aar/annee/cadeaux/"+gifts.get(i).getId()+"/update\"  method=\"post\">" +
			   	  	  	"<select name=\"valider\"><option value=\"true\">oui</option><option value=\"false\">non</option></select><input type=\"submit\" name=\"update\" value=\"ok\"/></form>" +
					  "</td><td><form action=\"http://localhost:9999/aar/annee/cadeaux/"+gifts.get(i).getId()+"/delete\"  method=\"post\">" +
			   	  	  	"<input type=\"submit\" name=\"supprimer\" value=\"supprimer\"/></form>" +
			   	      "</td></tr>"; 
		}
		
		String html = text_html_header+
					"<table class='table table-hover'>" +
					"<tr class='bandeau'><th>Nom</th><th>valider</th><th>fabriquer</th><th>Valider cadeaux</th><th>Supprimer</th></tr>"+result+
					"</table>" + text_html_footer;
		
		return html;
	}
	
	@GET
	@Path("/Enfants")
	@Produces(MediaType.TEXT_HTML)
	public String getChildsByALLHtml(@QueryParam("nom") String name,
								 @QueryParam("ville") String city,
								 @QueryParam("estSage") String IsWise){
		String result = "";		
		ChildDAO dao = new ChildDAO();
		List<Child> childs = null;
		childs = dao.getChildsByAll(name, city, IsWise);
		for(int i=0;i<childs.size();i++){
			result += "<tr class='bandeau'><td>" + childs.get(i).getName()+ "</td><td>"+
					  childs.get(i).getCity() + "</td><td>" + childs.get(i).getIsWise()+
				      "</td><td><a href=\"http://localhost:9999/aar/annee/enfants/"+childs.get(i).getId()+"/cadeaux\">voir</a>" +
				      "</td><td><form action=\"http://localhost:9999/aar/annee/enfants/"+childs.get(i).getId()+"/update\"  method=\"post\">" +
			   	  	  	"<select name=\"sage\"></option><option value=\"true\">oui</option><option value=\"false\">non</option></select><input type=\"submit\" name=\"update\" value=\"ok\"/></form>" +
				      "</td><td><form action=\"http://localhost:9999/aar/annee/enfants/"+childs.get(i).getId()+"/delete\"  method=\"post\">" +
			   	  	  	"<input type=\"submit\" name=\"supprimer\" value=\"supprimer\"/></form>" +
			   	      "</td></tr>";  
		}
		String html = text_html_header+
					"<table class='table table-hover'>" +
					"<tr class='bandeau'><th>Nom</th><th>Ville</th><th>Sage</th><th>Voir cadeaux</th><th>Enfant sage</th><th>Supprimer</th></tr>"+result+
					"</table>" + text_html_footer;
		
	    return html;
    }
	
	@GET
	@Path("/enfants")
	@Produces(MediaType.TEXT_HTML)
	public String getAllChildrenHtml(){
		String result = "";
		ChildDAO dao = new ChildDAO();
        List<Child> childs = dao.getAllChilds();
		for(int i=0;i<childs.size();i++){
			result += "<tr class='bandeau'><td>" + childs.get(i).getName()+ "</td><td>"+
					  childs.get(i).getCity() + "</td><td>" + childs.get(i).getIsWise()+
				      "</td><td><a href=\"http://localhost:9999/aar/annee/enfants/"+childs.get(i).getId()+"/cadeaux\">voir</a>" +
				      "</td><td><form action=\"http://localhost:9999/aar/annee/enfants/"+childs.get(i).getId()+"/update\"  method=\"post\">" +
			   	  	  	"<select name=\"sage\"><option value=\"true\">oui</option><option value=\"false\">non</option></select><input type=\"submit\" name=\"update\" value=\"ok\"/></form>" +
				      "</td><td><form action=\"http://localhost:9999/aar/annee/enfants/"+childs.get(i).getId()+"/delete\"  method=\"post\">" +
			   	  	  	"<input type=\"submit\" name=\"supprimer\" value=\"supprimer\"/></form>" +
			   	      "</td></tr>";  
		}
		String html = text_html_header+				
					"<table class='table table-hover'>" +
					"<tr class='bandeau'><th>Nom</th><th>Ville</th><th>Sage</th><th>Voir Cadeaux</th><th>Enfant sage</th><th>Supprimer</th></tr>"+result+
					"</table>" + text_html_footer;
	    return html;
	}
	
	@GET
	@Path("Cadeaux")
	@Produces(MediaType.TEXT_HTML)
	public String getGiftFromChildHtml( @QueryParam("nom") String name,
			 						@QueryParam("valider") String isValide,
			 						@QueryParam("fabriquer") String isCreate){
		String result = "";
		GiftDAO dao = new GiftDAO();
		List<Gift> gifts = dao.getGiftByAll(name, isValide, isCreate);
		for(int i=0;i<gifts.size();i++){
			result += "<tr class='bandeau'><td>"+gifts.get(i).getName()+"</td><td>"+gifts.get(i).getIsValid()+"</td><td>"+gifts.get(i).getIsCreate()+
					  "</td><td><form action=\"http://localhost:9999/aar/annee/cadeaux/"+gifts.get(i).getId()+"/update\"  method=\"post\">" +
			   	  	  	"<select name=\"valider\"></option><option value=\"true\">oui</option><option value=\"false\">non</option></select><input type=\"submit\" name=\"update\" value=\"ok\"/></form>" +
					  "</td><td><form action=\"http://localhost:9999/aar/annee/cadeaux/"+gifts.get(i).getId()+"/delete\"  method=\"post\">" +
			   	  	  	"<input type=\"submit\" name=\"supprimer\" value=\"supprimer\"/></form>" +
			   	      "</td></tr>"; 
		}
		String html = text_html_header+
					"<table class='table table-hover'>" +
					"<tr class='bandeau'><th>Nom</th><th>valider</th><th>fabriquer</th><th>Valider cadeaux</th><th>Supprimer</th></tr>"+result+
					"</table>" + text_html_footer;
		return html;
	}
	
	@POST
	@Path("/enfants")
	@Produces(MediaType.TEXT_HTML)
	public String newChildHtml(@FormParam("nom") String name,
						   @FormParam("ville") String city,
						   @FormParam("estSage") boolean isWise){
		String html ="";
		ChildDAO dao = new ChildDAO();
		long id = dao.addChild(name, city, isWise);
		html = text_html_header +
				"<form action=\"http://localhost:9999/aar/annee/enfants/"+id+"/cadeaux\" method=\"post\">" +
					"<table>" +
					"<tr class='bandeau'><td>Nom du Cadeau 1 :</td><td><input type=\"text\" name=\"cadeau1\"/></td></tr>" +
					"<tr class='bandeau'><td>Nom du Cadeau 2 :</td><td><input type=\"text\" name=\"cadeau2\"/></td></tr>" +
					"<tr class='bandeau'><td>Nom du Cadeau 3 :</td><td><input type=\"text\" name=\"cadeau3\"/></td></tr>" +
					"<tr class='bandeau'><tr class='bandeau'><td></td><td><input type=\"submit\" value=\"Ajouter Cadeau\"/></td></tr>" +
					"</table>" +
				"</form>" + text_html_footer;

	    return html;
	}
	
	@POST
	@Path("/enfants/{idEnfant}/cadeaux")
	@Produces(MediaType.TEXT_HTML)
	public String newGiftforChild(@PathParam("idEnfant") long idChild,
							  	  @FormParam("cadeau1") String name1,
							  	  @FormParam("cadeau2") String name2,
							  	  @FormParam("cadeau3") String name3){
		GiftDAO dao = new GiftDAO();
		ArrayList<String> gifts = new ArrayList<String>();
		gifts.add(name1);
		gifts.add(name2);
		gifts.add(name3);
		dao.addGift(gifts, idChild);
		String html = text_html_header + success_pre +
				
				"Votre Commande a ete bien envoyee au Pere Noel" + success_post + text_html_footer;
		
	    return html;
	}
	
	//@PUT
	@POST
	@Path("/enfants/{idEnfant}/update")
	@Produces(MediaType.TEXT_HTML)
	public String updateEnfant(@PathParam("idEnfant") long id,
							   @FormParam("sage") boolean isWise){
		ChildDAO dao = new ChildDAO();
		dao.updateChild(id, isWise);
		String html = text_html_header + success_pre +			
				"Cette operation a ete bien effectuee" + success_post + text_html_footer;
	    return html;
	}
	
	//@PUT
	@POST
	@Path("/cadeaux/{idCadeau}/update")
	@Produces(MediaType.TEXT_HTML)
	public String updateGiftFromChildValidk(@PathParam("idCadeau") long idGift,
									  @FormParam("valider") boolean isValid){
		GiftDAO dao = new GiftDAO();
		dao.updateGiftValid(idGift, isValid);
		String html = text_html_header + success_pre +			
				"Cette operation a ete bien effectuee" + success_post + text_html_footer;
	    return html;
	}
	
	//@DELETE
	@POST
    @Path("/enfants/{idEnfant}/delete")
	@Produces(MediaType.TEXT_HTML)
    public String deleteChild(@PathParam("idEnfant") long id) {
		ChildDAO dao = new ChildDAO();
		dao.deleteChild(id);
		String html = text_html_header + success_pre +
				"Cette operation a ete bien effectuee" + success_post +
				text_html_footer;
	    return html;
    }		
	
	//@DELETE
	@POST
	@Path("/cadeaux/{idCadeau}/delete")
	@Produces(MediaType.TEXT_HTML)
	public String deleteGift(@PathParam("idCadeau") long idGift){
		GiftDAO dao = new GiftDAO();
		dao.deleteGift(idGift);
		String html = text_html_header + success_pre +
				"Cette operation a ete bien effectuee" + success_post + text_html_footer;
	    return html;
	}
}